home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue25 / compress / COMPRESS.ZIP / ARC2MEM.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-07-09  |  5.2 KB  |  128 lines

  1. (* ARC2MEM.PAS for TCompress V3.0 (no change from V2.5 except para 5 in comments)
  2.  
  3. This code is designed to be dropped into the COMPDEMO application, adding
  4. one new routine (LoadArchivedFileToMemory) and replacing two existing ones
  5. (CDBImage1DragDrop and CDBMemo1DragDrop).
  6.  
  7. It gives an idea of a more efficient way to load compressed data from
  8. a file archive to a blob field, without using an intermediate file as
  9. COMPDEMO currently does.
  10.  
  11. However, it still decompresses the data on the way, so the most efficient
  12. approach is that shown ARC2BLOB.PAS which transfers the UNcompressed data
  13. as-is to a target compressed blob.
  14.  
  15. Keep in mind that both this and the ARC2BLOB code require manipulation of some
  16. of the TCompress data structures...
  17.  
  18. ** See the comment in ARC2BLOB.PAS about the new TCompress 3.0
  19. ExpandStreamFromArchive routine, which makes this kind of task simpler. This
  20. code is effectively rendered obsolete by the new routine, but we've kept it
  21. as an example of direct archive manipulation.
  22. *)
  23.  
  24.  
  25. { Example of Expanding a file DIRECTLY from an archive to another stream }
  26. procedure TForm1.LoadArchivedFileToMemory(var mem:Tmemorystream;filepath:String);
  27. var fs: TFilestream; { here we go... }
  28.     cfinfo: TCompressedFileInfo;
  29.     fheader: TCompressedFileHeader;
  30. begin
  31.   cfinfo := TCompressedFileInfo(FileList.objects[FileList.indexof(filepath)]);
  32.   fs:=TFileStream.Create(archivefile.text,fmOpenRead or fmShareExclusive); { just want to READ it... }
  33.   try
  34.      fs.seek(cfinfo.Position,0);        { find the file info start }
  35.      fs.read(fheader,sizeof(fheader));  { read the header }
  36.      fs.seek(fheader.filenameLength,1); { and skip the filename -- now at compressed data start }
  37.      mem.SetSize(cfinfo.FullSize);      { pre-set the size for fastest/cleanest results }
  38.      Compress1.DoExpand(mem,fs,cfinfo.CompressedSize,cfinfo.Fullsize, cfinfo.Checksum,
  39.                  cfinfo.CompressedMode,cfInfo.Locked); { Locked added V2.5 }
  40.   finally
  41.      fs.free
  42.   end;
  43. end;
  44.  
  45. { Examples of setting/loading/shifting image blobs using the above routine }
  46. procedure TForm1.CDBImage1DragDrop(Sender, Source: TObject; X, Y: Integer);
  47. var filepath: String;
  48.      mem: TMemoryStream; { for loading image from an archived file }
  49. begin
  50.    if Source=Sender then exit; { nowt to do }
  51.    if (Sender is TCDBImage) and (not Table1.active) then
  52.    begin
  53.      showmessage('Can''t do this unless table has been opened...');
  54.      exit;
  55.    end;
  56.  
  57.   Screen.Cursor := crHourGlass;
  58.   if (Source is TImage) and (Sender is TCDBImage) then
  59.      CDBImage1.picture.bitmap.Assign(Image1.Picture.bitmap)
  60.   else if (Source is TCDBImage) and (Sender is TImage) then
  61.      Image1.picture.bitmap.Assign(CDBImage1.Picture.Bitmap)
  62.   else
  63.   begin   { Have we got an image? }
  64.      filepath := '';
  65.      if (Source is TListBox) and (Listbox1.selcount = 1) then
  66.       filepath:=ListBox1.Items[Listbox1.ItemIndex] { archive list }
  67.      else if (Source is TFileListBox) and (FL.selcount=1) then
  68.         filepath:=FL.Items[FL.ItemIndex]; { file list }
  69.      if ExtractFileExt(filepath)<>'.bmp' then
  70.         showmessage('Must be a .BMP file...')
  71.      else                                     { ok, here we go... }
  72.         if Source is TFileListBox then { just load from file... }
  73.           if Sender is TImage then
  74.              Image1.Picture.Bitmap.LoadFromfile(filepath)
  75.           else
  76.              CDBImage1.Picture.Bitmap.LoadFromFile(filepath)
  77.         else { source must be our archive file... }
  78.         begin
  79.            mem:= TMemoryStream.create;
  80.            try
  81.              LoadArchivedFileToMemory(mem,filepath);
  82.              mem.seek(0,0);
  83.              if Sender is TImage then
  84.                Image1.Picture.Bitmap.LoadFromStream(mem)
  85.              else
  86.                CDBImage1.Picture.Bitmap.LoadFromStream(mem);
  87.            finally
  88.               mem.free
  89.            end;
  90.         end;
  91.   end;
  92.   if Table1.active and (Table1.State in [dsEdit]) then Table1.post; { save immediately if updated }
  93.   if not Image1.Picture.Bitmap.Empty then Memo1.visible := False; { got a piccy showing... }
  94.   Screen.Cursor := crDefault;
  95. end;
  96.  
  97. { Examples of setting/loading/shifting Memo blobs using LoadArchivedFileToMemory }
  98. procedure TForm1.CDBMemo1DragDrop(Sender, Source: TObject; X, Y: Integer);
  99. var filepath: String;
  100.      mem: TMemoryStream; { for loading text from an archived file }
  101. begin
  102.   filepath := ''; { in case fails }
  103.   if (Source is TListBox) and (Listbox1.selcount = 1) then
  104.    filepath:=ListBox1.Items[Listbox1.ItemIndex] { archive list }
  105.   else if (Source is TFileListBox) and (FL.selcount=1) then
  106.      filepath:=FL.Items[FL.ItemIndex]; { file list }
  107.   if ExtractFileExt(filepath)<>'.txt' then
  108.      showmessage('Must be a .TXT file...')
  109.   else begin                     { ok, here we go... }
  110.     Screen.Cursor := crHourGlass;
  111.    if Source is TFileListBox then
  112.      CDBMemo1.Lines.LoadfromFile(filepath)
  113.    else
  114.    begin
  115.      mem:= TMemoryStream.create;
  116.      try
  117.        LoadArchivedFileToMemory(mem,filepath);
  118.        mem.seek(0,0);
  119.        CDBMemo1.Lines.LoadfromStream(mem)
  120.      finally
  121.         mem.free
  122.      end;
  123.    end;
  124.   end;
  125.   if Table1.active and (Table1.State in [dsEdit]) then Table1.post; { save immediately if updated }
  126.   Screen.Cursor := crDefault;
  127. end;
  128.